package com.centaline.androidsalesblog.widgets;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.DrawableRequestBuilder;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.iml.RecyclerViewItemOnClickListener;
import com.centaline.androidsalesblog.model.businissobject.NewPropImgBo;
import com.centaline.androidsalesblog.model.businissobject.NewPropImgQueryBo;
import com.centanet.framework.base.BaseAdapter1;
import com.centanet.framework.glide.GlideLoad;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 新盘户型列表(新盘详情中使用)
 * Created by guilin on 16/2/24.
 */
public class NewEstHouseTypeView extends FrameLayout {

    private RecyclerView rv_house_type;
    private ArrayList<NewPropImgBo> newPropImgBos = new ArrayList<>();
    private ArrayList<String> imgUrlList = new ArrayList<>();
    private HouseTypeAdapter houseTypeAdapter;
    private DrawableRequestBuilder<String> requestBuilder;
    private RecyclerViewItemOnClickListener<Integer> recyclerViewItemOnClickListener;

    public NewEstHouseTypeView(Context context) {
        this(context, null);
    }

    public NewEstHouseTypeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NewEstHouseTypeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.recyclerview_new_est_house_type, this);
        rv_house_type = (RecyclerView) findViewById(R.id.rv_house_type);
        rv_house_type.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        houseTypeAdapter = new HouseTypeAdapter(context);
        rv_house_type.setAdapter(houseTypeAdapter);
    }

    /**
     * 初始化DrawableRequestBuilder
     */
    public void initRequestBuilder(DrawableRequestBuilder<String> requestBuilder) {
        this.requestBuilder = requestBuilder;
    }

    public void setRecyclerViewItemOnClickListener(RecyclerViewItemOnClickListener<Integer> recyclerViewItemOnClickListener) {
        this.recyclerViewItemOnClickListener = recyclerViewItemOnClickListener;
    }

    /**
     * 设置户型合集
     */
    public void setList(ArrayList<NewPropImgBo> newPropImgBoArrayList, ArrayList<String> stringArrayList) {
        newPropImgBos.clear();
        imgUrlList.clear();
        newPropImgBos.addAll(newPropImgBoArrayList);
        imgUrlList.addAll(stringArrayList);
        houseTypeAdapter.notifyDataSetChanged();
    }

    class HouseTypeAdapter extends BaseAdapter1<HouseTypeVH> {

        public HouseTypeAdapter(Context context) {
            super(context);
        }

        @Override
        protected int inflateByLayoutId() {
            return R.layout.item_new_est_house_type;
        }

        @Override
        protected HouseTypeVH viewHolder(View view) {
            return new HouseTypeVH(view);
        }

        @Override
        public void onBindViewHolder(final HouseTypeVH holder, int position) {
            final NewPropImgBo newPropImgBo = newPropImgBos.get(position);
            final NewPropImgQueryBo newPropImgQueryBo = newPropImgBo.getNewPropImgQueryBo();
            GlideLoad.load(new GlideLoad.Builder(requestBuilder, imgUrlList.get(position))
                    .placeHolder(R.drawable.ic_centa_square_logo)
                    .error(R.drawable.ic_centa_square_logo)
                    .openWifiSwitch()
                    .into(holder.img_house_type));
            holder.atv_house_type_title.setText(newPropImgQueryBo.getHouseTypeName());
            holder.atv_house_type_desc.setText(String.format(Locale.CHINA, "%d室%d厅 %d平",
                    newPropImgQueryBo.getRoomCnt(), newPropImgQueryBo.getHallCnt(), newPropImgQueryBo.getArea()));
            holder.itemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    recyclerViewItemOnClickListener.onItemOnClick(holder.getAdapterPosition(), 0);
                }
            });
        }

        @Override
        public int getItemCount() {
            return newPropImgBos.size();
        }
    }

    class HouseTypeVH extends RecyclerView.ViewHolder {

        public ImageView img_house_type;
        public AppCompatTextView atv_house_type_title, atv_house_type_desc;

        public HouseTypeVH(View itemView) {
            super(itemView);
            img_house_type = (ImageView) itemView.findViewById(R.id.img_house_type);
            atv_house_type_title = (AppCompatTextView) itemView.findViewById(R.id.atv_house_type_title);
            atv_house_type_desc = (AppCompatTextView) itemView.findViewById(R.id.atv_house_type_desc);
        }
    }
}
